const Router = require("koa-router");
const jwt = require("koa-jwt");

const {
  find,
  findById,
  create,
  update,
  del,
  authMe
} = require("../controllers/users");

const userRouter = new Router({ prefix: "/users" });
const secret = process.env.SECRET;
const auth = jwt({ secret });
// async (ctx, next) => {
//   const { authorization = "" } = ctx.request.header;
//   const token = authorization.replace("Bearer ", "");
//   try {
//     const user = jwt.verify(token, secret);
//     ctx.state.user = user;
//   } catch (err) {
//     ctx.throw(401, err.message);
//   }
//   await next();
// };

// 个人不能删除自己的账户，可以查看

userRouter.get("/", find);

userRouter.get("/auth/:token", authMe);

userRouter.post("/", auth, create);

userRouter.get("/:id", findById);

userRouter.patch("/:id", auth, update);

userRouter.delete("/:id", auth, del);

module.exports = userRouter;
